package com.android.launcher3.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.hotseat.CustomHotSeat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum AddWidgetPopWindow {

    INSTANT;

    private final static int MAX_ROW_COUNT = 7;

    private boolean isShow;

    private int height;

    public final List<WidgetData> widgetFull = new ArrayList<>();
    public final List<WidgetData> widgetHalf = new ArrayList<>();

    /**
     * 每个RecommendWidget都需要配置一个唯一ID， 当从未选过widget，根据Id来获取默认配置
     */
    public final Map<String, WidgetData> defaultWidget = new HashMap<>();


    //全部组件都配置在这里，根据需要修改图片和包名
    {
        //竖屏
//        widgetFull.add(new WidgetData(1, WidgetType.full, "com.hbo.hbonow", R.drawable.ic_hbmax, "HBO Max"));
//        widgetFull.add(new WidgetData(2, WidgetType.full, "com.disney.disneyplus", R.drawable.ic_fulll_diseny, "Disney+"));
//        widgetFull.add(new WidgetData(3, WidgetType.full, "com.google.android.apps.maps", R.drawable.ic_google_map, "Google Maps"));
//        widgetFull.add(new WidgetData(4, WidgetType.full, "com.waze", R.drawable.ic_waze, "waze"));
//
//        widgetFull.add(new WidgetData(5, WidgetType.full, "com.netflix.mediaclient", R.drawable.ic_netflix_full, "netflix"));
//        widgetFull.add(new WidgetData(6, WidgetType.full, "com.amazon.avod.thirdpartyclient", R.drawable.ic_primevideo_full, "Prime video"));
//        widgetFull.add(new WidgetData(7, WidgetType.full, "ru.yandex.yandexnavi", R.drawable.ic_yandex_full, "Yandex2"));
//        widgetFull.add(new WidgetData(8, WidgetType.full, "com.widget.speed", R.drawable.ic_speed_full, "Speed2"));
//
//        widgetHalf.add(new WidgetData(9, WidgetType.half, "com.spotify.music", R.drawable.ic_spotify, "Spotify"));
//        widgetHalf.add(new WidgetData(10, WidgetType.half, "com.disney.disneyplus", R.drawable.ic_diseny, "Disney+"));
//        widgetHalf.add(new WidgetData(11, WidgetType.half, "com.zhiliaoapp.musically", R.drawable.ic_tiktok, "Tiktok"));
//        widgetHalf.add(new WidgetData(12, WidgetType.half, "com.amazon.avod.thirdpartyclient", R.drawable.ic_prime_video, "Prime video"));
//
//        widgetHalf.add(new WidgetData(13, WidgetType.half, "com.netflix.mediaclient", R.drawable.ic_netflix, "Netflix"));
//        widgetHalf.add(new WidgetData(14, WidgetType.half, "com.hbo.hbonow", R.drawable.ic_hbo_half, "HBO Max"));
//        widgetHalf.add(new WidgetData(15, WidgetType.half, "com.hulu.plus", R.drawable.ic_hulu, "Hulu"));
//        widgetHalf.add(new WidgetData(16, WidgetType.half, "ru.yandex.yandexnavi", R.drawable.ic_yandex_half, "Yandex2"));
//
//        widgetHalf.add(new WidgetData(17, WidgetType.half, "com.waze", R.drawable.ic_waze_half, "waze"));
//        widgetHalf.add(new WidgetData(18, WidgetType.half, "com.google.android.apps.youtube.music", R.drawable.ic_yt_music, "YT Music"));
//        widgetHalf.add(new WidgetData(19, WidgetType.half, "com.google.android.apps.maps", R.drawable.ic_gmap, "Google Maps"));
//        widgetHalf.add(new WidgetData(20, WidgetType.half, "com.google.android.youtube", R.drawable.ic_youtube, "YouTube"));
//
//        widgetHalf.add(new WidgetData(21, WidgetType.split, null, R.drawable.ic_split, "Split"));
//
//        ///1 + true代表 组件1的上半区，1 + false 代表组件1的下半区，全屏组件只有1 + true
//        defaultWidget.put(1 + "true", new WidgetData(1, WidgetType.full, "com.hbo.hbonow", R.drawable.ic_hbmax, "HBO Max"));
//        defaultWidget.put(2 + "true", new WidgetData(10, WidgetType.half, "com.disney.disneyplus", R.drawable.ic_diseny, "Disney+"));
//        defaultWidget.put(2 + "false", new WidgetData(13, WidgetType.half, "com.netflix.mediaclient", R.drawable.ic_netflix, "Netflix"));
//        defaultWidget.put(3 + "true", new WidgetData(11, WidgetType.half, "com.zhiliaoapp.musically", R.drawable.ic_tiktok, "Tiktok"));
//        defaultWidget.put(3 + "false", new WidgetData(10, WidgetType.half, "com.disney.disneyplus", R.drawable.ic_diseny, "Disney+"));
//        defaultWidget.put(4 + "true", new WidgetData(6, WidgetType.full, "com.amazon.avod.thirdpartyclient", R.drawable.ic_primevideo_full, "Prime video"));
//        defaultWidget.put(5 + "true", new WidgetData(9, WidgetType.half, "com.spotify.music", R.drawable.ic_spotify, "Spotify"));
//        defaultWidget.put(5 + "false", new WidgetData(15, WidgetType.half, "com.hulu.plus", R.drawable.ic_hulu, "Hulu"));
//        defaultWidget.put(6 + "true", new WidgetData(3, WidgetType.full, "com.google.android.apps.maps", R.drawable.ic_google_map, "Google Maps"));


        //公版
        //竖屏
        widgetFull.add(new WidgetData(1, WidgetType.full, "com.hbo.hbonow", R.drawable.xq_hbo_full, "HBO Max"));
        widgetFull.add(new WidgetData(2, WidgetType.full, "com.disney.disneyplus", R.drawable.xq_disney_full, "Disney+"));
        widgetFull.add(new WidgetData(3, WidgetType.full, "com.google.android.apps.maps", R.drawable.xq_googlemap_full, "Google Maps"));
        widgetFull.add(new WidgetData(4, WidgetType.full, "com.waze", R.drawable.xq_waze_full, "waze"));

        widgetFull.add(new WidgetData(5, WidgetType.full, "com.netflix.mediaclient", R.drawable.xq_netflix_full, "Netflix"));
        widgetFull.add(new WidgetData(6, WidgetType.full, "com.amazon.avod.thirdpartyclient", R.drawable.xq_primevideo_full, "Prime video"));
        widgetFull.add(new WidgetData(7, WidgetType.full, "ru.yandex.yandexnavi", R.drawable.xq_yandex_full, "Yandex2"));
        widgetFull.add(new WidgetData(8, WidgetType.full, "com.spotify.music", R.drawable.xq_spotify_full, "Spotify"));
        widgetFull.add(new WidgetData(9, WidgetType.full, "com.zhiliaoapp.musically", R.drawable.xq_tiktok_full, "Tiktok"));
        widgetFull.add(new WidgetData(10, WidgetType.full, "com.hulu.plus", R.drawable.xq_hulu_full, "Hulu"));

        widgetHalf.add(new WidgetData(11, WidgetType.half, "com.spotify.music", R.drawable.xq_spotify_half, "Spotify"));
        widgetHalf.add(new WidgetData(12, WidgetType.half, "com.disney.disneyplus", R.drawable.xq_disney_half, "Disney+"));
        widgetHalf.add(new WidgetData(13, WidgetType.half, "com.zhiliaoapp.musically", R.drawable.xq_tiktok_half, "Tiktok"));
        widgetHalf.add(new WidgetData(14, WidgetType.half, "com.amazon.avod.thirdpartyclient", R.drawable.xq_primevideo_half, "Prime video"));

        widgetHalf.add(new WidgetData(15, WidgetType.half, "com.netflix.mediaclient", R.drawable.xq_netflix_half, "Netflix"));
        widgetHalf.add(new WidgetData(16, WidgetType.half, "com.hbo.hbonow", R.drawable.xq_hbo_half, "HBO Max"));
        widgetHalf.add(new WidgetData(17, WidgetType.half, "com.hulu.plus", R.drawable.xq_hulu_half, "Hulu"));
        widgetHalf.add(new WidgetData(18, WidgetType.half, "ru.yandex.yandexnavi", R.drawable.xq_yandex_half, "Yandex2"));

        widgetHalf.add(new WidgetData(19, WidgetType.half, "com.waze", R.drawable.xq_waze_half, "waze"));
        widgetHalf.add(new WidgetData(20, WidgetType.half, "com.google.android.apps.youtube.music", R.drawable.xq_yt_music, "YT Music"));
        widgetHalf.add(new WidgetData(21, WidgetType.half, "com.google.android.apps.maps", R.drawable.xq_googlemap_half, "Google Maps"));
        widgetHalf.add(new WidgetData(22, WidgetType.half, "com.google.android.youtube", R.drawable.xq_youtube, "YouTube"));

        widgetHalf.add(new WidgetData(23, WidgetType.split, null, R.drawable.ic_split, "Split"));

        ///1 + true代表 组件1的上半区，1 + false 代表组件1的下半区，全屏组件只有1 + true
        defaultWidget.put(1 + "true", new WidgetData(1, WidgetType.full, "com.hbo.hbonow", R.drawable.xq_hbo_full, "HBO Max"));
        defaultWidget.put(2 + "true", new WidgetData(12, WidgetType.half, "com.disney.disneyplus", R.drawable.xq_disney_half, "Disney+"));
        defaultWidget.put(2 + "false", new WidgetData(15, WidgetType.half, "com.netflix.mediaclient", R.drawable.xq_netflix_half, "Netflix"));
        defaultWidget.put(3 + "true", new WidgetData(13, WidgetType.half, "com.zhiliaoapp.musically", R.drawable.xq_tiktok_half, "Tiktok"));
        defaultWidget.put(3 + "false", new WidgetData(12, WidgetType.half, "com.waze", R.drawable.xq_waze_half, "waze"));
        defaultWidget.put(4 + "true", new WidgetData(14, WidgetType.full, "com.amazon.avod.thirdpartyclient", R.drawable.xq_primevideo_full, "Prime video"));
        defaultWidget.put(5 + "true", new WidgetData(11, WidgetType.half, "com.spotify.music", R.drawable.xq_spotify_half, "Spotify"));
        defaultWidget.put(5 + "false", new WidgetData(17, WidgetType.half, "com.hulu.plus", R.drawable.xq_hulu_half, "Hulu"));
        defaultWidget.put(6 + "true", new WidgetData(3, WidgetType.full, "com.google.android.apps.maps", R.drawable.xq_googlemap_full, "Google Maps"));

    }


    public void show(@NonNull Launcher launcher, boolean isFull, ChooseWidgetListener chooseWidgetListener) {

        List<WidgetData> widgetDataList = isFull ? widgetFull : widgetHalf;

        PopupWindow popupWindow = new PopupWindow(launcher);
        View container = LayoutInflater.from(launcher).inflate(isFull ? R.layout.add_widget_pop_window : R.layout.add_widget_pop_window_half, null);
        RecyclerView recyclerView = container.findViewById(R.id.recycler);

        if (isFull) {
            recyclerView.setLayoutManager(new LinearLayoutManager(launcher, LinearLayoutManager.HORIZONTAL, false));
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(launcher, 2, LinearLayoutManager.HORIZONTAL, false));
        }

        recyclerView.setAdapter(new WidgetAdapter(launcher, widgetDataList, isFull, new WidgetAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (chooseWidgetListener != null) {
                    chooseWidgetListener.onChoose(widgetDataList.get(position));
                }
                popupWindow.dismiss();
            }

            @Override
            public void onClickOutSide(int position) {
                popupWindow.dismiss();
            }
        }));

        popupWindow.setContentView(container);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(getPopHeight(launcher));
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(false);
        popupWindow.showAsDropDown(launcher.findViewById(R.id.custom_hot_seat));
        popupWindow.setOnDismissListener(() -> isShow = false);
        container.setOnClickListener(v -> popupWindow.dismiss());
        isShow = true;

    }

    private int getPopHeight(Launcher launcher) {

        if (height <= 0) {
            DisplayMetrics displayMetrics = launcher.getResources().getDisplayMetrics();
            height = displayMetrics.heightPixels - Utilities.pxFromDp(Utilities.isLongStripScreen(launcher) ? (CustomHotSeat.CUSTOM_HOT_SEAT_STRIP_SCREEN_HEIGHT + 10) : (CustomHotSeat.CUSTOM_HOT_SEAT_HEIGHT + 10), displayMetrics);

            int maxHeight = Utilities.pxFromDp(400, displayMetrics);
            if (height > maxHeight) {
                height = maxHeight;
            }
        }

        return height;
    }


    public WidgetData getWidgetData(boolean isFull, int widgetId) {

        if (widgetId <= 0) return null;

        List<WidgetData> data = isFull ? widgetFull : widgetHalf;

        for (int i = 0; i < data.size(); i++) {

            WidgetData widgetData = data.get(i);
            if (widgetData.id == widgetId) return widgetData;

        }

        return null;
    }

    public WidgetData getDefaultData(int recommendId, boolean isTopHalf) {
        return defaultWidget.get(recommendId + String.valueOf(isTopHalf));
    }

    public static class WidgetAdapter extends RecyclerView.Adapter<WidgetAdapter.VH> {

        public WidgetAdapter(Context context, List<WidgetData> data, boolean isFull, OnItemClickListener onItemClickListener) {
            this.context = context;
            this.data = data;
            this.isFull = isFull;
            this.onItemClickListener = onItemClickListener;
        }

        private Context context;
        private List<WidgetData> data;
        private boolean isFull;

        private OnItemClickListener onItemClickListener;


        @NonNull
        @Override
        public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(context).inflate(isFull ? R.layout.layout_full_widget : R.layout.layout_half_widget, null);
            return new VH(view);
        }

        @Override
        public void onBindViewHolder(@NonNull VH vh, int i) {
            if (data.get(i).imageResId > 0) {
                vh.imageView.setImageResource(data.get(i).imageResId);
            }
            vh.textView.setText(Utilities.trim(data.get(i).title));
            vh.imageLayout.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(i);
                }
            });
            vh.itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onClickOutSide(i);
                }
            });
        }

        @Override
        public int getItemCount() {
            return data == null ? 0 : data.size();
        }

        public static class VH extends RecyclerView.ViewHolder {

            ImageView imageView;
            TextView textView;
            View imageLayout;

            public VH(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.image);
                textView = itemView.findViewById(R.id.text);
                imageLayout = itemView.findViewById(R.id.layout_image);
            }
        }

        public interface OnItemClickListener {
            void onItemClick(int position);

            void onClickOutSide(int position);
        }
    }


    public interface ChooseWidgetListener {
        void onChoose(WidgetData widgetData);
    }

    public static class WidgetData {

        ///存储标记使用
        int id;
        WidgetType type;
        String packageName;
        @DrawableRes
        int imageResId;
        String title;

        public WidgetData(int id, WidgetType type, String packageName, int imageResId, String title) {
            this.id = id;
            this.type = type;
            this.packageName = packageName;
            this.imageResId = imageResId;
            this.title = title;

        }
    }

    public enum WidgetType {

        full, half, split

    }


}
